import 'dart:io';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:path_provider/path_provider.dart';
import 'package:localstorage/localstorage.dart';

class StorageManager {
  /// app全局配置 eg:theme
  static SharedPreferences? sharedPreferences;

  /// 临时目录 eg: cookie
  static Directory? temporaryDirectory;

  /// 必备数据的初始化操作
  /// 由于是同步操作会导致阻塞,所以应尽量减少存储容量
  static Future init() async {
    // async 异步操作
    // sync 同步操作
    temporaryDirectory = await getTemporaryDirectory();
    sharedPreferences = await SharedPreferences.getInstance();
    return initLocalStorage();
  }

  static void setItem(String key, dynamic value) {
    localStorage.setItem(key, value);
  }

  static String? getItem(String key) {
    return localStorage.getItem(key);
  }

  static void clear() {
    sharedPreferences?.clear();
    localStorage.clear();
  }

  static LocalStorage get ls {
    return localStorage;
  }
}
